package cz.acies.uniql.utils;

import java.util.Enumeration;
import java.util.Hashtable;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


import cz.acies.uniql.dialect.Dialect;
import cz.acies.uniql.dialect.MySQLDialect;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.UniQLExpr;
import cz.acies.uniql.sql.element.UniQLColumn;
import cz.acies.uniql.sql.element.UniQLCondition;
import cz.acies.uniql.sql.element.UniQLValue;
import cz.acies.uniql.sql.functions.SQLFunc;
import cz.acies.utils.StringUtils;

public class XmlTranslator
{

	public static void main(String[] args) {
		try {
			
			MySQLDialect dialect = new MySQLDialect();
			
			String field =
			"<SQLFunc name=\"concat\">" +
				"<UniQLColumn table=\"${alias}\" name=\"STREET_CORRES\"/>" +
				"<SQLFunc name=\"if\">" +
					"<UniQLCondition>" +
						"<SQLFunc name=\"to_varchar\">" +
							"<UniQLColumn table=\"${alias}\" name=\"STREET_CORRES\"/>" +
						"</SQLFunc>" +
						"<operand operand=\"=\"/>" +
						"<UniQLValue value=\"''\"/>" +
					"</UniQLCondition>" +
					"<UniQLValue value=\"''\"/>" +
					"<UniQLValue value=\"', '\"/>" +
				"</SQLFunc>" +
				"<UniQLColumn table=\"${alias}\" name=\"ZIP_CORRES\"/>" +
				"<UniQLValue value=\"' '\"/>" +
				"<UniQLColumn table=\"${alias}\" name=\"CITY_CORRES\"/>" +
			"</SQLFunc>";

			Hashtable patterns = new Hashtable();
			patterns.put("${alias}", "TBL2");
			UniQLExpr expr = xmlToExpr(dialect, field, patterns);
			System.out.println("expr = "+expr.translate());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public XmlTranslator() {
	}
	
	public static UniQLExpr xmlToExpr(Dialect dialect, Element node) throws Exception {
		String nodename = node.getName();
		UniQLExpr expr = null; 
		if (nodename.trim().equals("UniQLColumn")) {
			expr = new UniQLColumn(dialect).xmlToExpr(node);
		} else if (nodename.trim().equals("UniQLValue")) {
			expr = new UniQLValue(dialect).xmlToExpr(node);
		} else if (nodename.trim().equals("UniQLCondition")) {
			expr = new UniQLCondition(dialect).xmlToExpr(node);
		} else if (nodename.trim().equals("SQLFunc")) {
			expr = new SQLFunc(dialect).xmlToExpr(node);
// -----
		} else if (nodename.trim().equals("SelectQuery")) {
//			expr = new SelectQuery(dialect).xmlToExpr(node);
		}	
// -----		
		return expr;
	}
	
	public static UniQLExpr xmlToExpr(Dialect dialect, String xml, Hashtable patterns) throws Exception {
		UniQLExpr expr = null;
		try {
			if (patterns != null) {
				for (Enumeration e = patterns.keys() ; e.hasMoreElements() ;) {
					String from = (String) e.nextElement();
					String to = (String) patterns.get(from);
					xml = StringUtils.replace(xml, from, to);
				}	
			}
			Document doc = DocumentHelper.parseText(xml);
			Element node = doc.getRootElement();
			expr = xmlToExpr(dialect, node);
			System.out.println("XmlTranslator.xmlToExpr()expr = "+expr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expr;
	}
}
